
#include <Arduino.h>
#include "Switch.h"
// 自定义模块
#include "Senser/SenserConfig.h"
#include "MqttModule/MyMqtt.h"

bool switch1HighLevel = false; // 继电器1高电平触发
bool switch2HighLevel = false; // 继电器2高电平触发
bool switch3HighLevel = false; // 继电器3高电平触发

void send_switch_discovery_message() {
    String discovery_topic = "homeassistant/switch/esp32_relay/config";

    String payload = R"(
    {
        "name": "ESP32 Relay",
        "command_topic": "home/esp32/relay/set",
        "state_topic": "home/esp32/relay/state",
        "unique_id": "esp32_relay",
        "device": {
            "identifiers": ["esp32"],
            "name": "ESP32",
            "model": "ESP32",
            "manufacturer": "ESP"
        }
    }
    )";

    mqClient.publish(discovery_topic.c_str(), payload.c_str(), true);
}

void initSwitch(){
    // 初始化开关引脚
    pinMode(switchPin1, OUTPUT );
    // 初始化开关引脚
    pinMode(switchPin2, OUTPUT);
    // 初始化开关引脚
    pinMode(switchPin3, OUTPUT);

    // 设置初始状态
    digitalWrite(switchPin1, switch1HighLevel ? LOW : HIGH);
    // 设置初始状态
    digitalWrite(switchPin2, switch2HighLevel ? LOW : HIGH);
    // 设置初始状态
    digitalWrite(switchPin3, switch2HighLevel ? LOW : HIGH);
}

//模拟按下开机按键
void powerControl(){
    digitalWrite(switchPin1, switch1HighLevel ? HIGH : LOW); // 继电器吸合
    delay(1000);
    digitalWrite(switchPin1, switch1HighLevel ? LOW : HIGH); // 继电器释放
}

void openSwitch(){
    digitalWrite(switchPin2, switch2HighLevel ? HIGH : LOW); // 继电器吸合
}

void closeSwitch(){
    digitalWrite(switchPin2, switch2HighLevel ? LOW : HIGH); // 继电器释放
}